package cn.uniqueinfo.dp.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-04 15:05:20
 * @description: 验证信息处理
 */
public class ValidUtil {

    /**获取错误信息*/
    public static String errorInfo(BindingResult bindingResult){

        List<String> errList = new ArrayList<>();

        if(bindingResult.hasErrors()){
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for(ObjectError error : errorList){
                // 获取校验的字段名称
                // DefaultMessageSourceResolvable dmsr = (DefaultMessageSourceResolvable) error.getArguments()[0];
                // sb.append(dmsr.getCode()+"="+error.getDefaultMessage());
                // sb.append(error.getDefaultMessage());
                errList.add(error.getDefaultMessage());
            }
        }

        return StringUtils.join(errList,";");
    }
}
